<?

class StatsSiteHelper {

	 public $mysql;
	 
	 public function __construct($mysql){
		  $this->mysql = $mysql;
	 }
	 
	 public function get(){
		  $mysql = $this->mysql;
		  $query = "SELECT valid, count(*) as nb FROM projet GROUP BY valid;";
		  $res = $mysql->query($query);
		  if($mysql->errno != 0) throw new Exception ("Erreur MySQL : ".$mysql->error);
		  
		  $stats["projet"]["VALIDE"] = 0;
		  $stats["projet"]["EN_VALIDATION"] = 0;
		  $stats["projet"]["DONE"] = 0;
		  $stats["projet"]["REJETTE"] = 0;
		  $stats["projet"]["ATTRIBUTED"] = 0;
		  $stats["projet"]["EN_ATTRIBUTION"] = 0;
		  $stats["projet"]["WORKING"] = 0;
		  
		  while($line = $res->fetch_object()){
				$stats["projet"][$line->valid]+=$line->nb;
		  }
		  
		  
		  $query = "SELECT valid, count(*) as nb FROM offre GROUP BY valid;";
		  $res = $mysql->query($query);
		  if($mysql->errno != 0) throw new Exception ("Erreur MySQL : ".$mysql->error);
		  
		  $stats["offre"]["VALIDE"] = 0;
		  $stats["offre"]["REJETTE"] = 0;
		  
		  while($line = $res->fetch_object()){
				$stats["offre"][$line->valid]+=$line->nb;
		  }
		  
		  $query = "SELECT state, count(*) as nb FROM facture GROUP BY state;";
		  $res = $mysql->query($query);
		  if($mysql->errno != 0) throw new Exception ("Erreur MySQL : ".$mysql->error);
		  
		  $stats["facture"]["PENDING"] = 0;
		  $stats["facture"]["PAYED"] = 0;
		  
		  while($line = $res->fetch_object()){
				$stats["facture"][$line->state]+=$line->nb;
		  }
		  
		  $query = "SELECT state, sum(montant) as somme FROM facture GROUP BY state;";
		  $res = $mysql->query($query);
		  if($mysql->errno != 0) throw new Exception ("Erreur MySQL : ".$mysql->error);
		  
		  $stats["CA"]["PAYED"] = 0;
		  $stats["CA"]["PENDING"] = 0;
		  
		  while($line = $res->fetch_object()){
				$stats["CA"][$line->state]+=$line->somme;
		  }
		  
		  
		  
		  return $stats;
	 }	 

}